using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Scene.Video;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Media;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace MicroMsg.UI.Page
{
	public class VideoPlayerPage : BasePage
	{
		private const string TAG = "VideoPlayerPage";
		private DispatcherTimer playTimer;
		private int totalSeconds;
		private double leftSeconds;
		private bool isPause;
		private BitmapImage previewBitmapImage;
		private IsolatedStorageFileStream isoVideoFile;
		private ChatMsg chatMsg;
		private ChatVideoItem chatElement;
		private EventWatcher videoDownloadWatcher;
		private ApplicationBar mBar;
		internal Grid LayoutRoot;
		internal Grid viewLayout;
		internal StackPanel videoLayout;
		internal ClickableLayout layoutVideoPlayer;
		internal MediaElement VideoPlayer;
		internal Button layoutLeftTime;
		internal TextBlock txtLeftTime;
		internal TextBlock txtVedioInfo;
		internal StackPanel imageLayout;
		internal Image previewImage;
		internal TextBlock txtVedioInfo1;
		internal Image playButton;
		internal Canvas loadLayout;
		internal Image loadingImage;
		internal ProgressBar loadProcessBar;
		internal TextBlock txtProcess;
		internal TextBlock txtDownloadFaild;
		private bool _contentLoaded;
		public VideoPlayerPage()
		{
			this.InitializeComponent();
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("VideoPlayerPage", "OnPageIn");
			this.InitWatcher();
			if (object_0 != null && object_0[0] is ChatMsg)
			{
				this.chatMsg = (object_0[0] as ChatMsg);
			}
			if (object_0 != null && object_0[1] is ChatVideoItem)
			{
				this.chatElement = (object_0[1] as ChatVideoItem);
			}
			if (this.chatMsg != null && this.chatElement != null)
			{
				if (this.chatElement.mTrans == null)
				{
					return;
				}
				string msgThumbnail = ChatMsgMgr.getMsgThumbnail(this.chatMsg);
				string text = string.Format("{0}KB", (double)(this.chatElement.mTrans.nTotalDataLen * 10 / 1024) / 10.0);
				this.totalSeconds = this.chatElement.mTrans.nDuration;
				this.VideoPlayer.add_MediaEnded(new RoutedEventHandler(this.VideoPlayerMediaEnded));
				if (msgThumbnail != null && msgThumbnail.get_Length() > 0)
				{
					this.previewBitmapImage = ImageCacheMgr.loadFromFile(msgThumbnail);
					this.previewImage.set_Source(this.previewBitmapImage);
				}
				this.leftSeconds = (double)this.totalSeconds;
				this.txtVedioInfo.set_Text(text + "  " + this.getTimeShow((double)this.totalSeconds));
				this.txtVedioInfo1.set_Text(text + "  " + this.getTimeShow((double)this.totalSeconds));
				this.txtLeftTime.set_Text(this.getTimeShow(this.leftSeconds));
				DownloadVideoContext.ProgressInfo progress = ServiceCenter.sceneDownloadVideo.getProgress(this.chatMsg.nMsgSvrID);
				if (this.chatMsg.nStatus != 2 || progress != null)
				{
					if (!this.chatMsg.IsSender())
					{
						goto IL_217;
					}
				}
				try
				{
					this.viewLayout.set_Visibility(0);
					this.loadLayout.set_Visibility(1);
					string strPath = this.chatMsg.strPath;
					this.isoVideoFile = new IsolatedStorageFileStream(strPath, 3, 1, IsolatedStorageFile.GetUserStoreForApplication());
					this.VideoPlayer.SetSource(this.isoVideoFile);
					this.VideoPlayer.set_AutoPlay(true);
					this.playButtonClick(null, null);
					return;
				}
				catch (Exception)
				{
					Log.smethod_5("VideoPlayerPage", "Video file not find.");
					return;
				}
				IL_217:
				int num = 0;
				if (progress != null && progress.totalLength > 0)
				{
					num = progress.gotLength * 100 / progress.totalLength;
				}
				this.loadLayout.set_Visibility(0);
				this.viewLayout.set_Visibility(1);
				this.loadProcessBar.set_Value((double)num);
				ServiceCenter.sceneDownloadVideo.doSence(this.chatMsg.nMsgSvrID);
			}
		}
		private void InitWatcher()
		{
			if (this.videoDownloadWatcher == null)
			{
				this.videoDownloadWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onDownloadEvent));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DOWNLOADVIDEO_PROGRESS, this.videoDownloadWatcher);
			}
		}
		private void UnitWatcher()
		{
			if (this.videoDownloadWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DOWNLOADVIDEO_PROGRESS, this.videoDownloadWatcher);
				this.videoDownloadWatcher = null;
			}
		}
		private void onDownloadEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			Log.smethod_5("VideoPlayerPage", "onDownloadEvent");
			DownloadVideoContext.ProgressInfo progressInfo = evtArgs.mObject as DownloadVideoContext.ProgressInfo;
			if (progressInfo.msgSvrId != this.chatMsg.nMsgSvrID)
			{
				return;
			}
			if (progressInfo != null)
			{
				if (progressInfo.status == 2)
				{
					this.viewLayout.set_Visibility(0);
					this.loadLayout.set_Visibility(1);
					string strPath = this.chatMsg.strPath;
					this.isoVideoFile = new IsolatedStorageFileStream(strPath, 3, 1, IsolatedStorageFile.GetUserStoreForApplication());
					this.VideoPlayer.SetSource(this.isoVideoFile);
					this.VideoPlayer.set_AutoPlay(true);
					this.playButtonClick(null, null);
					return;
				}
				if (progressInfo.status == 1)
				{
					this.loadProcessBar.set_Visibility(1);
					this.txtProcess.set_Visibility(1);
					this.txtDownloadFaild.set_Visibility(0);
					return;
				}
				if (progressInfo.status == 0)
				{
					int num = 0;
					if (progressInfo.totalLength > 0)
					{
						num = progressInfo.gotLength * 100 / progressInfo.totalLength;
					}
					this.loadProcessBar.set_Value((double)num);
					this.txtProcess.set_Text(strings.ChatVideoDownloading + string.Format("( {0}KB/{1}KB )", progressInfo.gotLength / 1024, progressInfo.totalLength / 1024));
				}
			}
		}
		public void VideoPlayerMediaEnded(object sender, RoutedEventArgs e)
		{
			this.endPlay();
		}
		private void endPlay()
		{
			if (this.playTimer != null)
			{
				this.playTimer.Stop();
			}
			this.txtLeftTime.set_Text(this.getTimeShow(0.0));
			this.playButton.set_Visibility(0);
			if (this.previewBitmapImage != null)
			{
				this.previewImage.set_Source(this.previewBitmapImage);
			}
			this.videoLayout.set_Visibility(1);
			this.imageLayout.set_Visibility(0);
			this.leftSeconds = (double)this.totalSeconds;
		}
		protected override void OnPageOut()
		{
			Log.smethod_5("VideoPlayerPage", "OnPageOut");
			this.UnitWatcher();
			if (this.isoVideoFile != null)
			{
				this.isoVideoFile.Close();
			}
		}
		private string getTimeShow(double seconds)
		{
			int num = (int)seconds / 60;
			int num2 = (int)seconds % 60;
			string text = string.Concat(num);
			string text2 = string.Concat(num2);
			if (num2 < 10)
			{
				text2 = "0" + text2;
			}
			return text + ":" + text2;
		}
		private void playButtonClick(object sender, RoutedEventArgs e)
		{
			if (this.isoVideoFile == null)
			{
				MMMessageBox.Show(strings.ChatVideoNotFound, true, "确定");
				return;
			}
			this.playButton.set_Visibility(1);
			this.imageLayout.set_Visibility(1);
			this.videoLayout.set_Visibility(0);
			this.isPause = false;
			this.txtLeftTime.set_Text(this.getTimeShow(this.leftSeconds));
			if (this.playTimer == null)
			{
				this.playTimer = new DispatcherTimer();
				this.playTimer.set_Interval(new TimeSpan(0, 0, 1));
				this.playTimer.add_Tick(new EventHandler(this.updateLeftTime));
			}
			if (this.leftSeconds > 0.0)
			{
				this.leftSeconds -= 0.5;
			}
			if (!this.playTimer.get_IsEnabled())
			{
				this.playTimer.Start();
			}
			this.VideoPlayer.Play();
		}
		private void videoPlayer_Click(object sender, RoutedEventArgs e)
		{
			if (!this.VideoPlayer.get_CanPause())
			{
				return;
			}
			if (!this.isPause)
			{
				this.VideoPlayer.Pause();
				this.isPause = true;
				if (this.playTimer != null)
				{
					this.playTimer.Stop();
				}
				this.playButton.set_Visibility(0);
			}
		}
		public void updateLeftTime(object sender, EventArgs e)
		{
			if (!this.isPause)
			{
				if (this.leftSeconds > 0.0)
				{
					this.leftSeconds -= 1.0;
					this.txtLeftTime.set_Text(this.getTimeShow(this.leftSeconds));
					if (this.layoutLeftTime.get_Visibility() == 1 && this.layoutVideoPlayer.get_ActualHeight() > 0.0)
					{
						this.layoutLeftTime.set_Margin(new Thickness(0.0, this.layoutVideoPlayer.get_ActualHeight() * -1.0, 10.0, 0.0));
						this.layoutLeftTime.set_Visibility(0);
						this.txtVedioInfo.set_Visibility(0);
						return;
					}
				}
				else
				{
					this.playTimer.Stop();
				}
			}
		}
		private DoubleAnimation createAnimation(double from, double double_0, UIElement target, string property, uint duration)
		{
			new Storyboard();
			DoubleAnimation doubleAnimation = new DoubleAnimation();
			doubleAnimation.set_From(new double?(from));
			doubleAnimation.set_To(new double?(double_0));
			doubleAnimation.set_Duration(TimeSpan.FromMilliseconds(duration));
			Storyboard.SetTarget(doubleAnimation, target);
			Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(property, new object[0]));
			return doubleAnimation;
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
		}
		public IApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Opacity(0.7);
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("/Resource/Img/Chat/save.png", 2));
			applicationBarIconButton.set_Text(strings.COM_Save);
			applicationBarIconButton.add_Click(new EventHandler(this.saveButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton);
			return this.mBar;
		}
		private void saveButton_Click(object sender, EventArgs e)
		{
			if (this.isoVideoFile != null)
			{
				using (this.isoVideoFile)
				{
					MediaLibrary mediaLibrary = new MediaLibrary();
					mediaLibrary.SavePicture(Util.getNowMilliseconds() + ".mp4", this.isoVideoFile);
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/VideoPlayerPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.viewLayout = (Grid)base.FindName("viewLayout");
			this.videoLayout = (StackPanel)base.FindName("videoLayout");
			this.layoutVideoPlayer = (ClickableLayout)base.FindName("layoutVideoPlayer");
			this.VideoPlayer = (MediaElement)base.FindName("VideoPlayer");
			this.layoutLeftTime = (Button)base.FindName("layoutLeftTime");
			this.txtLeftTime = (TextBlock)base.FindName("txtLeftTime");
			this.txtVedioInfo = (TextBlock)base.FindName("txtVedioInfo");
			this.imageLayout = (StackPanel)base.FindName("imageLayout");
			this.previewImage = (Image)base.FindName("previewImage");
			this.txtVedioInfo1 = (TextBlock)base.FindName("txtVedioInfo1");
			this.playButton = (Image)base.FindName("playButton");
			this.loadLayout = (Canvas)base.FindName("loadLayout");
			this.loadingImage = (Image)base.FindName("loadingImage");
			this.loadProcessBar = (ProgressBar)base.FindName("loadProcessBar");
			this.txtProcess = (TextBlock)base.FindName("txtProcess");
			this.txtDownloadFaild = (TextBlock)base.FindName("txtDownloadFaild");
		}
	}
}
